package com.example.testwebsocket.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

@Configuration
@Slf4j
public class ServerEndpointConfigurator extends ServerEndpointConfig.Configurator {

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        // 尝试获取到当前Websocket链接的HttpSession
        HttpSession httpSession = (HttpSession)request.getHttpSession();


        if (httpSession != null) {
            // session id
            log.info("sessionId={}", httpSession.getId());
            // 读取session域中存储的数据
            log.info("name={}", httpSession.getAttribute("name"));

            sec.getUserProperties().put(HttpSession.class.getName(),httpSession);

        }

        super.modifyHandshake(sec, request, response);

    }




}
